//
//  AddNoteViewController.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/27.
//

import UIKit
//import IQKeyboardManagerSwift

class AddNoteViewController: BaseViewController {

    var writeDate: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()

//        IQKeyboardManager.shared.isEnabled = true
        setupUI()
    }
    
    private func setupUI(){
        
        view.addSubviews([
            titleLabel,
            backButton,
            saveButton,
            iconImgV,
            timeLabel,
            noteV
        ])
        
        backButton.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalToSuperview().offset(CGFloat.statusHeight)
            make.width.equalTo(56)
            make.height.equalTo(44)
        }
        saveButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.top.equalToSuperview().offset(CGFloat.statusHeight)
            make.height.equalTo(44)
            make.width.equalTo(56)
        }
        titleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalToSuperview().offset(CGFloat.statusHeight)
            make.height.equalTo(44)
        }
        iconImgV.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(CGFloat.navi_height + 15)
            make.height.width.equalTo(24)
        }
        timeLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-15)
            make.centerY.equalTo(iconImgV)
            make.leading.equalTo(iconImgV.snp.trailing).offset(10)
        }
        noteV.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalToSuperview().offset(-16)
            make.height.equalTo(185)
            make.top.equalTo(iconImgV.snp.bottom).offset(20)
        }
    }
    
    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc private func clickSaveButton(){
        
        guard let text = noteV.text, text.count > 0 else {
            showToast("Please enter the note content")
            return
        }
        showToast("Save successful~")
        let entry = Entry(content: text)
        appendEntry(entry, toDate: writeDate, forKey: "dailyLogs")
        self.navigationController?.popViewController(animated: true)
    }

    private lazy var titleLabel: UILabel = {
        let v = UILabel()
        v.textColor = .black
        v.font = UIFont.boldSystemFont(ofSize: 18)
        v.text = "ZentriNot"
        v.textAlignment = .center
        return v
    }()
    private lazy var backButton: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage(UIImage(named: "black_back"), for: .normal)
        v.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        return v
    }()
    private lazy var saveButton: UIButton = {
        let v = UIButton()
        v.setTitle("Save", for: .normal)
        v.setTitleColor(UIColor(hexString: "#0E84FF"), for: .normal)
        v.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        v.addTarget(self, action: #selector(clickSaveButton), for: .touchUpInside)
        return v
    }()
    private lazy var iconImgV: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "today")
        return v
    }()
    private lazy var timeLabel: UILabel = {
        let v = UILabel()
        v.textColor = .black
        v.font = UIFont.boldSystemFont(ofSize: 18)
        v.text = writeDate
        return v
    }()
    private lazy var noteV: UITextView = {
        let v = UITextView()
        v.backgroundColor = UIColor(hexString: "#F5F9FA")
        v.layer.cornerRadius = 8
        v.layer.masksToBounds = true
        v.font = UIFont.systemFont(ofSize: 14)
        v.textColor = UIColor(hexString: "#8A8B8D")
        v.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        return v
    }()
}
